/**
 * problem_019.c
 * Copyright (C) 2011-03-19 - xrose
 * --
 *  * 1 Jan 1900 was a Monday.
    * Thirty days has September,
      April, June and November.
      All the rest have thirty-one,
      Saving February alone,
      Which has twenty-eight, rain or shine.
      And on leap years, twenty-nine.
    * A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
 */

#include <stdio.h>
int day_of_week(int _date, int _month, int _year);
int first_day_year(int year);
int is_leap_year(int _year);

int main (int argc, char *argv[])
{
//    printf("__%d\n", first_day_year(2000));
//    printf("__+_+__%d\n", day_of_week(1, 1, 1900));

    int i, j;
    int count = 0;
    for (i=1901; i<=2000; i++)
    {
        for(j=1; j<=12; j++)
        {
            if(day_of_week(1, j, i) == 7)
            {
                count ++;
                printf("_1_%d_%d\n", j, i);
            }
        }
    }
    printf("__%d__+_+__\n", count);
    return 0;
}
int day_of_week(int _date, int _month, int _year)
{
    //char dow[9];    //monday, tuesday, wenesday, thursday, friday, saturday, sunday
    int _dow = 0;          //1:monday, 2:tuesday, 3:wenesday, 4:thursday, 5:friday, 6:saturday, 7:sunday
    int _doy = 0;
    int i;
    //1-1-1900 is monday
    //check leap-year: divisible by 4 but 100 except divisible by 400
    //a leap-year has 366 days - Feb has 29 days
    //a non leap year has 365 days - Feb has 28 days
    int ndays_month[12];    //0:Jan, 1:Feb, 2:Mar, 3:Apr, 4:May, 5:Jun, 6:Jul, 7:Aug, 8:Sep, 9:Oct, 10:Nov, 11:Dec

    ndays_month[0] = ndays_month[2] = ndays_month[4] = ndays_month[6] = ndays_month[7] = ndays_month[9] = ndays_month[11] = 31;
    ndays_month[3] = ndays_month[5] = ndays_month[8] = ndays_month[10] = 30;
    if(is_leap_year(_year))
    {
        ndays_month[1] = 29;
    } else
    {
        ndays_month[1] = 28;
    }
    for(i=0; i<_month -1; i++)
    {
        _doy +=ndays_month[i];
    }
    _doy = _doy + _date -1; //the first date of month is assigned 0
    _dow = _doy%7 + first_day_year(_year);
    return _dow;
}
int first_day_year(int year)
{
    int i, tmp;
    int dis_1_1_1900 = 0;
    //return 1-7(0) <--> monday-sunday
    if(year > 1900)
    {
        for(i=1900; i<year; i++)
        {
            if(is_leap_year(i))
            {
//                printf("__||__");
                dis_1_1_1900 +=366;
            } else
            {
//                printf("__||__");
                dis_1_1_1900 +=365;
            }
        }
        tmp = (dis_1_1_1900%7 + 1);
    } else if(year == 1900)
    {
        tmp = 1;
    } else
    {
        for(i=year; i < 1900; i++)
        {
            if(is_leap_year(i))
            {
                dis_1_1_1900 +=366;
            } else
            {
                dis_1_1_1900 +=365;
                printf("__%d\n", dis_1_1_1900);
            }
        }
        tmp = (dis_1_1_1900%7 -1);
    }
    if(tmp == 0)
    {
        return 7;
    } else
    {
        return tmp;
    }
}
int is_leap_year(int _year)
{
    if(((_year%4 == 0)&&(_year%100 != 0))||(_year%400 == 0))
    {
        return 1;   //leap year
    } else
    {
        return 0;   //non leap year
    }
}
